package com.apes.autoparts.parts.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.MapUtil;
import com.apestech.framework.util.StringUtil;
import lombok.Getter;
import lombok.Setter;
import org.apache.poi.ss.formula.functions.T;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lishikun on 2021/5/28 11:37
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = EpcSearchLogItem.CONST_TABLE_NAME)
public class EpcSearchLogItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "EPC_SEARCH_LOG_ITEM";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "EPC搜索日志明细")
    @Comment("唯一码")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "EPC_SEARCH_LOG_ID", referencedColumnName = "id")
    @Comment("日志编码")
    private EpcSearchLog epcSearchLog;

    @Column(name = "OE")
    @Comment("PART_CODE")
    private String partCode;

    @Column(name = "PART_NAME")
    @Comment("零件名称")
    private String partName;

//    @Column(name = "PJMC")
//    @Comment("配件名称")
//    private String PJMC;
//    @Column(name = "PJID")
//    @Comment("配件名称")
//    private String PJID;

    @Column(name = "PJ", length = 4000)
    @Comment("配件名称")
    private String PJ;

    @Column(name = "IMAGE_SN")
    @Comment("图号")
    private String imageSN;

    @Column(name = "IMAGE_URLS")
    @Comment("EPC图")
    private String imageUrls;

    @Column(name = "MAIN_GROUP_ID")
    @Comment("主组id")
    private String mainGroupId;

    @Column(name = "MAIN_GROUP_NAME")
    @Comment("主组名称")
    private String mainGroupName;

    @Column(name = "SUB_GROUP_ID")
    @Comment("子组id")
    private String subGroupId;

    @Column(name = "SUB_GROUP_NAME")
    @Comment("子组名称")
    private String subGroupName;

    @Column(name = "PART_ID")
    @Comment("零件ID")
    private String partId;

    @Column(name = "price", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("4S售价")
    private double price = 0;

    @Column(name = "cost", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("4S采购价")
    private double cost = 0;

    @Transient
    private List PJList;

    @Transient // 主组
    private String mainGroup;

    public String getMainGroup(){
        return "[" + (mainGroupId != null ? mainGroupId : "") + "]" + mainGroupName;
    }

    @Transient // 子组
    private String subGroup;

    public String getSubGroup(){
        return "[" + (subGroupId != null ? subGroupId : "") + "]" + subGroupName;
    }

    public List getPJList() {
        return convertToList(PJ);
    }

    public List<Map> convertToList(String source) {
        if (StringUtil.isEmpty(source)) return null;
        List<Map> categories = new ArrayList<>();
        String[] rows = source.split(";");
        for (String row : rows) {
            if (row.contains(":")) {
                categories.add(MapUtil.mapper("id", String.valueOf(StringUtils.split(row, ":")[0]), "name", String.valueOf(StringUtils.split(row, ":")[1])));
            }
        }
        return categories;
    }


    public String convertToString(List pjs) {
        StringBuffer sb = new StringBuffer();
        for (Object o : pjs) {
            JSONObject pj = (JSONObject)o;
            if (sb.length() > 0) {
                sb.append(";");
            }
            sb.append(pj.getString("PJMCID"));
            sb.append(":");
            sb.append(pj.getString("PJMC"));  // 格式 id:name;
        }
        return sb.toString();
    }
}
